home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / image / indexcol.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  9.1 KB  |  282 lines

  1. /*
  2.  * @(#)IndexColorModel.java    1.8 95/10/07 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.awt.image;
  21.  
  22. /**
  23.  * A ColorModel class that specifies a translation from pixel values
  24.  * to alpha, red, green, and blue color components for pixels which
  25.  * represent indices into a fixed colormap.  An optional transparent
  26.  * pixel value can be supplied which indicates a completely transparent
  27.  * pixel, regardless of any alpha value recorded for that pixel value.
  28.  * This color model is similar to an X11 PseudoColor visual.
  29.  *
  30.  * @see ColorModel
  31.  *
  32.  * @version    1.8 10/07/95
  33.  * @author     Jim Graham
  34.  */
  35. public class IndexColorModel extends ColorModel {
  36.     private byte red[];
  37.     private byte green[];
  38.     private byte blue[];
  39.     private byte alpha[];
  40.     private int map_size;
  41.  
  42.     private int transparent_index;
  43.  
  44.     /**
  45.      * Construct an IndexColorModel from the given arrays of red,
  46.      * green, and blue components.  Pixels described by this color
  47.      * model will all have alpha components of 255 (fully opaque).
  48.      * All of the arrays specifying the color components must have
  49.      * at least the specified number of entries.
  50.      * @param bits    The number of bits each pixel occupies.
  51.      * @param size    The size of the color component arrays.
  52.      * @param r        The array of red color components.
  53.      * @param g        The array of green color components.
  54.      * @param b        The array of blue color components.
  55.      */
  56.     public IndexColorModel(int bits, int size,
  57.                byte r[], byte g[], byte b[]) {
  58.     this(bits, size, r, g, b, -1);
  59.     }
  60.  
  61.     /**
  62.      * Construct an IndexColorModel from the given arrays of red,
  63.      * green, and blue components.  Pixels described by this color
  64.      * model will all have alpha components of 255 (fully opaque),
  65.      * except for the indicated transparent pixel.  All of the arrays
  66.      * specifying the color components must have at least the specified
  67.      * number of entries.
  68.      * @param bits    The number of bits each pixel occupies.
  69.      * @param size    The size of the color component arrays.
  70.      * @param r        The array of red color components.
  71.      * @param g        The array of green color components.
  72.      * @param b        The array of blue color components.
  73.      * @param trans    The index of the transparent pixel.
  74.      */
  75.     public IndexColorModel(int bits, int size,
  76.                byte r[], byte g[], byte b[], int trans) {
  77.     super(bits);
  78.     if ((bits > 8) || (size > (1 << bits))) {
  79.         throw new ArrayIndexOutOfBoundsException();
  80.     }
  81.     map_size = size;
  82.     red = new byte[256];
  83.     System.arraycopy(r, 0, red, 0, size);
  84.     green = new byte[256];
  85.     System.arraycopy(g, 0, green, 0, size);
  86.     blue = new byte[256];
  87.     System.arraycopy(b, 0, blue, 0, size);
  88.     alpha = null;
  89.     setTransparentPixel(trans);
  90.     }
  91.  
  92.     /**
  93.      * Construct an IndexColorModel from the given arrays of red,
  94.      * green, blue and alpha components.  All of the arrays specifying
  95.      * the color components must have at least the specified number
  96.      * of entries.
  97.      * @param bits    The number of bits each pixel occupies.
  98.      * @param size    The size of the color component arrays.
  99.      * @param r        The array of red color components.
  100.      * @param g        The array of green color components.
  101.      * @param b        The array of blue color components.
  102.      * @param a        The array of alpha value components.
  103.      */
  104.     public IndexColorModel(int bits, int size,
  105.                byte r[], byte g[], byte b[], byte a[]) {
  106.     this(bits, size, r, g, b, -1);
  107.     alpha = new byte[256];
  108.     System.arraycopy(a, 0, alpha, 0, size);
  109.     }
  110.  
  111.     /**
  112.      * Construct an IndexColorModel from a single arrays of packed
  113.      * red, green, blue and optional alpha components.  The array
  114.      * must have enough values in it to fill all of the needed
  115.      * component arrays of the specified size.
  116.      * @param bits    The number of bits each pixel occupies.
  117.      * @param size    The size of the color component arrays.
  118.      * @param cmap    The array of color components.
  119.      * @param start    The starting offset of the first color component.
  120.      * @param hasalpha    Indicates whether alpha values are contained in
  121.      *            the cmap array.
  122.      */
  123.     public IndexColorModel(int bits, int size, byte cmap[], int start,
  124.                boolean hasalpha) {
  125.     this(bits, size, cmap, start, hasalpha, -1);
  126.     }
  127.  
  128.     /**
  129.      * Construct an IndexColorModel from a single arrays of packed
  130.      * red, green, blue and optional alpha components.  The specified
  131.      * transparent index represents a pixel which will be considered
  132.      * entirely transparent regardless of any alpha value specified
  133.      * for it.  The array must have enough values in it to fill all
  134.      * of the needed component arrays of the specified size.
  135.      * @param bits    The number of bits each pixel occupies.
  136.      * @param size    The size of the color component arrays.
  137.      * @param cmap    The array of color components.
  138.      * @param start    The starting offset of the first color component.
  139.      * @param hasalpha    Indicates whether alpha values are contained in
  140.      *            the cmap array.
  141.      * @param trans    The index of the fully transparent pixel.
  142.      */
  143.     public IndexColorModel(int bits, int size, byte cmap[], int start,
  144.                boolean hasalpha, int trans) {
  145.     // REMIND: This assumes the ordering: RGB[A]
  146.     super(bits);
  147.     if ((bits > 8) || (size > (1 << bits))) {
  148.         throw new ArrayIndexOutOfBoundsException();
  149.     }
  150.     map_size = size;
  151.     red = new byte[256];
  152.     green = new byte[256];
  153.     blue = new byte[256];
  154.     if (hasalpha) {
  155.         alpha = new byte[256];
  156.     }
  157.     int j = start;
  158.     for (int i = 0; i < size; i++) {
  159.         red[i] = cmap[j++];
  160.         green[i] = cmap[j++];
  161.         blue[i] = cmap[j++];
  162.         if (hasalpha) {
  163.         alpha[i] = cmap[j++];
  164.         }
  165.     }
  166.     setTransparentPixel(trans);
  167.     }
  168.  
  169.     /**
  170.      * Returns the size of the color component arrays in this IndexColorModel.
  171.      */
  172.     public int getMapSize() {
  173.     return map_size;
  174.     }
  175.  
  176.     /**
  177.      * Returns the index of the transparent pixel in this IndexColorModel
  178.      * or -1 if there is no transparent pixel.
  179.      */
  180.     public int getTransparentPixel() {
  181.     return transparent_index;
  182.     }
  183.  
  184.     /**
  185.      * Copy the array of red color components into the given array.  Only
  186.      * the initial entries of the array as specified by getMapSize() will
  187.      * be written.
  188.      */
  189.     public void getReds(byte r[]) {
  190.     System.arraycopy(red, 0, r, 0, map_size);
  191.     }
  192.  
  193.     /**
  194.      * Copy the array of green color components into the given array.  Only
  195.      * the initial entries of the array as specified by getMapSize() will
  196.      * be written.
  197.      */
  198.     public void getGreens(byte g[]) {
  199.     System.arraycopy(green, 0, g, 0, map_size);
  200.     }
  201.  
  202.     /**
  203.      * Copy the array of blue color components into the given array.  Only
  204.      * the initial entries of the array as specified by getMapSize() will
  205.      * be written.
  206.      */
  207.     public void getBlues(byte b[]) {
  208.     System.arraycopy(blue, 0, b, 0, map_size);
  209.     }
  210.  
  211.     /**
  212.      * Copy the array of alpha transparency values into the given array.  Only
  213.      * the initial entries of the array as specified by getMapSize() will
  214.      * be written.
  215.      */
  216.     public void getAlphas(byte a[]) {
  217.     if (alpha != null) {
  218.         System.arraycopy(alpha, 0, a, 0, map_size);
  219.     } else {
  220.         for (int i = 0; i < map_size; i++) {
  221.         if (i == transparent_index) {
  222.             a[i] = 0;
  223.         } else {
  224.             a[i] = (byte) 255;
  225.         }
  226.         }
  227.     }
  228.     }
  229.  
  230.     private void setTransparentPixel(int trans) {
  231.     if (trans >= map_size || trans < 0) {
  232.         trans = -1;
  233.     }
  234.     transparent_index = trans;
  235.     }
  236.  
  237.     /**
  238.      * Return the red color compoment for the specified pixel in the
  239.      * range 0-255.
  240.      */
  241.     public int getRed(int pixel) {
  242.     return red[pixel];
  243.     }
  244.  
  245.     /**
  246.      * Return the green color compoment for the specified pixel in the
  247.      * range 0-255.
  248.      */
  249.     public int getGreen(int pixel) {
  250.     return green[pixel];
  251.     }
  252.  
  253.     /**
  254.      * Return the blue color compoment for the specified pixel in the
  255.      * range 0-255.
  256.      */
  257.     public int getBlue(int pixel) {
  258.     return blue[pixel];
  259.     }
  260.  
  261.     /**
  262.      * Return the alpha transparency value for the specified pixel in the
  263.      * range 0-255.
  264.      */
  265.     public int getAlpha(int pixel) {
  266.     return ((pixel == transparent_index) ? 0 : ((alpha != null)
  267.                             ? alpha[pixel]
  268.                             : 255));
  269.     }
  270.  
  271.     /**
  272.      * Return the color of the pixel in the default RGB color model.
  273.      * @see ColorModel#getRGBdefault
  274.      */
  275.     public int getRGB(int pixel) {
  276.     return (getAlpha(pixel) << 24)
  277.         | ((red[pixel] & 0xff) << 16)
  278.         | ((green[pixel] & 0xff) << 8)
  279.         | (blue[pixel] & 0xff);
  280.     }
  281. }
  282.